//
//  ScenicGuiderListCell.m
//  ImGuider
//
//  Created by llt on 2017/4/18.
//  Copyright © 2017年 imguider. All rights reserved.
//

#import "ScenicGuiderListCell.h"
#import "StarRateView.h"
#import "PlayerManager.h"
#import <AVFoundation/AVFoundation.h>
#import <SDWebImage/UIButton+WebCache.h>
#import "GuiderDetailVC.h"
#import "ScenicDescribeVC.h"
#import "TagView.h"
#import "PlayerManager.h"
#import "LinePreDetailVC.h"
@interface ScenicGuiderListCell()<PlayerManagerDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *headImageView;
@property (weak, nonatomic) IBOutlet UILabel *scenicTitle;
@property (weak, nonatomic) IBOutlet StarRateView *starView;
@property (weak, nonatomic) IBOutlet UILabel *scenicGuiderLabel;
@property (weak, nonatomic) IBOutlet UILabel *scenicCountLabel;
@property (weak, nonatomic) IBOutlet UILabel *listenCountLabel;
@property (weak, nonatomic) IBOutlet UILabel *costLabel;

@property (nonatomic, strong)PlayerManager * playManager;

@property (nonatomic, strong)LineModel * lineModel;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *tagViewW;

@property (weak, nonatomic) IBOutlet TagView *tagView;
@property (weak, nonatomic) IBOutlet UILabel *lineTimeLabel;
@property (weak, nonatomic) IBOutlet UIImageView *timeImageView;

@property (weak, nonatomic) IBOutlet UIImageView *vipTagImageView;
@property (weak, nonatomic) IBOutlet UILabel *tryListen;
@property (weak, nonatomic) IBOutlet UILabel *describeLabel;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *describeLabelTop;

@property (nonatomic,copy)NSString * guiderID;
//@property (nonatomic, strong)NSTimer * timer;
@property (nonatomic,copy)NSString * playPath;
@end

@implementation ScenicGuiderListCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    self.starView.backgroundColor = [UIColor whiteColor];
    self.headImageView.clipsToBounds = YES;
    self.headImageView.userInteractionEnabled = YES;
    self.tagViewW.constant = 0;
    self.tryListen.text = MYLocalizedString(@"PUBLIC_PLAY_TRIALS", nil);
    
    
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(goGuiderDetailVC)];
    [self.headImageView addGestureRecognizer:tap];
    
    UITapGestureRecognizer * tapCell = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(goScenicDetailVC)];
    [self.contentView addGestureRecognizer:tapCell];
    self.headImageView.layer.borderWidth = 0.5;
    self.headImageView.layer.borderColor = kContentColor.CGColor;
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(ListChangePlayStateWith:) name:kPlayManagerState object:nil];
    self.costLabel.textColor = kThemeColor;
    self.playBtn.layer.cornerRadius = 5;
    self.playBtn.clipsToBounds = YES;
    
    self.playManager = [PlayerManager shareManeger];
    self.tagView.tagViewWidth = self.tagViewW;
    
    self.selectionStyle = UITableViewCellSelectionStyleNone;

}

- (void)goScenicDetailVC {
    
    if (self.superVC && self.lineModel) {
        
        LinePreDetailVC *detailVC = [IGUtils loadViewController:@"LinePreDetailVC" storyboard:@"LineDetail"];
        detailVC.lineid = self.lineModel.lineid;
        
        [AppManager sharedManager].lineid = self.lineModel.lineid;
        
        self.superVC.NoAnimation = YES;
        [self.superVC.navigationController pushViewController:detailVC animated:YES];
    }
}

-(void)goGuiderDetailVC {
    if (self.superVC) {
        GuiderDetailVC * vc = [GuiderDetailVC new];
        vc.guiderID = self.guiderID;
        self.superVC.NoAnimation = YES;
        [self.superVC.navigationController pushViewController:vc animated:YES];
        [MobClick event:@"6_1_3"];
    }
}

//-(void)setFrame:(CGRect)frame
//{
//    frame.size.height -= 10;
//    [super setFrame:frame];
//}
- (IBAction)playMusic:(UIButton *)sender {
    
    if (self.playPath && self.playBtn.selected && [PlayerManager shareManeger].isPlay) {
        [[PlayerManager shareManeger] pause];
        
        [MobClick event:@"6_0_4"];
        return;
    }
    
    [MobClick event:@"6_0_3"];

    if ([[PlayerManager shareManeger].currentURL isEqualToString:self.playPath] && ![PlayerManager shareManeger].isPlay) {
        [[PlayerManager shareManeger] play];
    }else {
        
        [[PlayerManager shareManeger] playMusic:self.playPath withIsPlay:YES];
    }
    
    self.playManager.delegate = self;
}

- (void)updateProgressWith:(CGFloat)value {
    
    [self.playBtn setProgress:value];
}

- (void)ListChangePlayStateWith:(NSNotification *)notif
{
    NSInteger state = [notif.userInfo[kPlayManagerState] integerValue];
    NSString  * url = notif.userInfo[kCurrentPlayURL];
    if (![url isEqualToString:self.playPath]) {
        return;
    }
    switch (state) {
        case AVPlayerPlayStatePreparing:
        case AVPlayerPlayStateBeigin:
        case AVPlayerPlayStatePlaying:
        {
            self.playBtn.selected = YES;
//            if (!_timer) {
//                [self playMusicAnimation];
//            }
            
            [self.playBtn  startSpinProgressBackgroundLayer];
        }
            break;
        case AVPlayerPlayStatePause:
        case AVPlayerPlayStateNotPlay:
        case AVPlayerPlayStateNotKnow:
        {
            self.playBtn.selected = NO;
            [self stop];
            
        }
            break;
        case AVPlayerPlayStateEnd:
        {
            self.playBtn.selected = NO;
            [self stop];
        }
            break;
        default:
        {
        }
            break;
    }
    
}



//- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
//    IGLog(@"cell-began");
//    [super touchesBegan:touches withEvent:event];
//   // [self.nextResponder touchesBegan:touches withEvent:event];
//}
//
//- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
//    [super touchesBegan:touches withEvent:event];
//    IGLog(@"cell-end");
//}
//
//- (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
//    
//    IGLog(@"cell-cancell");
//}

- (void)stop {
//    [self.timer setFireDate:[NSDate distantFuture]];
//    [self.timer invalidate];
    [self.playBtn setProgress:0];
    self.headImageView.transform = CGAffineTransformIdentity;
//    self.timer = nil;
}

-(void)playMusicAnimation {
    
    PlayerItem *playItem = [[UIApplication sharedApplication].keyWindow viewWithTag:1000];
    if (playItem) {
        [playItem.layer removeAllAnimations];
        [playItem removeFromSuperview];
    }
    
//    _timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(rotation) userInfo:nil repeats:YES];
//    [[NSRunLoop mainRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];
//    [_timer setFireDate:[NSDate distantPast]];
}

- (void)rotation {
    self.headImageView.transform = CGAffineTransformRotate(self.headImageView.transform,M_PI/100);
}
- (void)configDataWithScenicGuiderListModel:(LineModel *)model {

    
    self.lineModel = model;
    [self.starView setScore:model.score.floatValue/10];
    [self.headImageView setImageURL:[NSURL URLWithString:model.guide.headimg] placeholderImage:[UIImage imageNamed:@"default"]];
    self.scenicTitle.text = model.linename;
    self.scenicGuiderLabel.text = model.guide.realname;
    self.listenCountLabel.text = [NSString stringWithFormat:@"%@",model.visits ? model.visits :@(0)];
    self.lineTimeLabel.text = [NSString stringWithFormat:@"%@min",model.timelabel];
    self.scenicCountLabel.text = model.recordcount;
    
    if (SCREEN_WIDTH < 370) {
        
        self.timeImageView.hidden = YES;
        self.lineTimeLabel.hidden = YES;
    }
    
    if (model.linedesc.length > 0) {
        self.describeLabelTop.constant = 10;
    } else {
        self.describeLabelTop.constant = 0;
    }
    self.describeLabel.attributedText = [IGUtils attributedStringText:model.linedesc font:Font(12) color:kContentColor lineSpacing:lineSpace];
    
    
    if (model.bought) {
        self.costLabel.text = MYLocalizedString(@"SITEPLAY_PURCHASED", nil);
        self.costLabel.font = Font(12);
    }else {
    self.costLabel.text = model.price;
    self.costLabel.font = Font(14);
    }
    
    if (model.labels.length > 0 ) {
        
    }
    
    self.tagView.tagsArray = model.tagArray;
    self.guiderID = model.guide.guideid;
    self.playPath = model.playpath;
    
    self.vipTagImageView.hidden = !model.guide.vip;
}

- (void)dealloc {
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
